import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '../stores/user'
import HomeView from '../views/HomeView.vue'
import NPCView from '../views/NPCView.vue'
import SystemView from '../views/SystemView.vue'

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/npc',
    name: 'npc',
    component: NPCView,
    meta: { requiresNPC: true }
  },
  {
    path: '/system',
    name: 'system',
    component: SystemView,
    meta: { requiresSystem: true }
  },
  {
    path: '/editor',
    name: 'editor',
    component: () => import('../views/NPCView.vue'),
    meta: { requiresNPC: true }
  },
  {
    path: '/tasks',
    name: 'tasks',
    component: () => import('../views/TaskView.vue'),
    meta: { requiresSystem: true }
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 路由守卫 - 身份验证
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  
  if (to.meta.requiresNPC && !userStore.isNPC) {
    next('/')
  } else if (to.meta.requiresSystem && !userStore.isSystem) {
    next('/')
  } else {
    next()
  }
})

export default router